package edu.ustb.mis.dm.simpleTest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.poi.ss.usermodel.Workbook;
import edu.ustb.mis.dm.model.AccuracyModel;
import edu.ustb.mis.dm.model.ResultModel;
import edu.ustb.mis.excel.utils.ExcelExporter;
import edu.ustb.mis.excel.utils.ExcelImporter;

/**
 * @author saya 2011-5-24
 */
public class ResultAy {
	private static final String PATH = "output/eapx/process/";

	private ExcelImporter<AccuracyModel> importer = new ExcelImporter<AccuracyModel>(AccuracyModel.class);

	private List<ResultModel> rmlist = new ArrayList<ResultModel>();

	public static void main(String[] args) throws Exception {
		ResultAy ra = new ResultAy();
		ra.imp();
	}

	private void export(final Collection<ResultModel> rmlist) throws Exception {
		final OutputStream out = new FileOutputStream("output/eapx/result.xlsx");
		final ExcelExporter<ResultModel> ex = new ExcelExporter<ResultModel>();
		final Workbook wb = ex.exportExcel("综合分析", rmlist, "2007");
		wb.write(out);
		out.flush();
		out.close();
	}

	public void imp() {
		File file = new File(PATH);
		File[] files = file.listFiles();
		try {
			for (File f : files) {
				//System.out.println(f.getName());
				double b = Double.valueOf(f.getName().substring(8, f.getName().indexOf(".xlsx")));
				List<AccuracyModel> result = (ArrayList<AccuracyModel>)importer.importExcel(f, "2007");
				ResultModel rm = cal(result, b);
				rmlist.add(rm);
			}
			export(rmlist);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ResultModel cal(List<AccuracyModel> result, double b) {
		double correct = 0;
		int all = 0;
		double acc = 0;
		double types = 0;
		int size = result.size();
		for (AccuracyModel accuracyModel : result) {
			correct += accuracyModel.getCorrectNum();
			all += accuracyModel.getAllNum();
			acc += accuracyModel.getAccuracy();
			types += accuracyModel.getTypes();
		}
		return new ResultModel(b, correct / size, all / size, acc / size, types / size);
	}
}
